package org.chen.config;

import lombok.extern.slf4j.Slf4j;
import org.chen.handler.CrawlerWebSocketHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

// WebSocket配置类
@Configuration
@EnableWebSocket
@Slf4j
public class WebSocketConfig implements WebSocketConfigurer {
    
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        log.info("Registering WebSocket handlers...");
        registry.addHandler(crawlerWebSocketHandler(), "/ws/crawler")
                .setAllowedOrigins("https://admin.chen2586.top");  // 指定具体的前端地
        log.info("WebSocket handlers registered successfully");
    }
    
    @Bean
    public CrawlerWebSocketHandler crawlerWebSocketHandler() {
        return new CrawlerWebSocketHandler();
    }
}